import { ref, type Ref } from "vue";
import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw, Router } from "vue-router";
import { vueMetaEnv } from "@/settings";

export const LoginPage = () => import("@/views/login/index.vue");
export const WechatSliloginPage = () => import("@/views/wechat/slilogin.vue");
export const WechatRegisterPage = () => import("@/views/wechat/register.vue");
export const AlipayloginPage = () => import("@/views/alipay/alilogin.vue");
export const UnionPayloginPage = () => import("@/views/unionpay/login.vue");
export const HomePage = () => import("@/views/home/index.vue");
export const AboutPage = () => import("@/views/common/about/index.vue");
export const Error401Page = () => import("@/views/error-page/401.vue");
export const Error404Page = () => import("@/views/error-page/404.vue");
export const RedirectPage = () => import("@/views/redirect/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: RedirectPage
      }
    ]
  },
  {
    path: "/401",
    component: Error401Page,
    meta: { hidden: true }
  },
  {
    path: "/404",
    component: Error404Page,
    meta: { hidden: true }
  },
  // 微信环境登录
  {
    path: "/wechat/slilogin/:tenant/:componentAppid",
    component: WechatSliloginPage,
    meta: { hidden: true }
  },
  // 微信环境注册
  {
    path: "/wechat/register/:tenant/:componentAppid",
    name: "Register",
    component: WechatRegisterPage,
    meta: { hidden: true }
  },
  // 阿里支付宝环境登录
  {
    path: "/alipay/alilogin",
    component: AlipayloginPage,
    meta: { hidden: true }
  },
  // 云闪付环境登录
  {
    path: "/unionpay/login",
    component: UnionPayloginPage,
    meta: { hidden: true }
  }
];

// 动态路由样例
export const testRoutes: RouteRecordRaw[] = [
  {
    path: "/test",
    meta: { roleCode: "admin" },
    children: [
      {
        path: "about",
        component: AboutPage,
        name: "about",
        meta: { title: "关于", icon: "user", permCode: "user:about" }
      }
    ]
  },
  {
    path: "/login",
    component: LoginPage,
    meta: { hidden: true }
  },
  {
    path: "/",
    component: HomePage,
    name: "Home", // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
    // https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
    meta: {
      title: "Home",
      icon: "homepage",
      affix: true,
      keepAlive: true,
      alwaysShow: false
    }
  }
];

//import { addDynamicRoute } from "jmash-core";
//addDynamicRoute(testRoutes);

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(vueMetaEnv.APP_BASE_URL),
  routes: constantRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
});

/**
 * 创建路由
 */
const routerRef: Ref = ref(router);

/**
 * 重置路由
 */
export function resetRouter() {
  // routerRef.value.replace({ path: "/" });
  router.replace({ path: "/" });
}

export function updateRouter(newRouter: Router) {
  routerRef.value = newRouter;
}

/**
 * 创建新路由.
 */
export function createNewRouter(base?: string) {
  const newRouter1 = createRouter({
    history: createWebHistory(base),
    routes: constantRoutes as RouteRecordRaw[],
    // 刷新时，滚动条位置还原
    scrollBehavior: () => ({ left: 0, top: 0 })
  });
  routerRef.value = newRouter1;
  return routerRef;
}

export default routerRef;
